<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/6/17 下午4:38
 * +-------------------------------------------------------------
 */

namespace app\common\validate;

use think\Validate;

class LoginValidate extends Validate
{
    protected $rule = [
            'username' => 'require|max:20',
            'password' => 'require|max:20',
            'captcha'  => 'require|captcha',
            'phone'    => 'require|max:11',
            'scene_id' => 'require',
    ];

    protected $message = [
            'username.require' => '用户名不能为空',
            'username.max'     => '用户名长度不能超过20个字符',
            'password.require' => '密码不能为空',
            'password.max'     => '密码长度不能超过20个字符',
            'captcha.require'  => '验证码不能为空',
            'captcha.captcha'  => '验证码错误',
            'phone.require'    => '手机号不能为空',
            'phone.max'        => '手机号长度不能超过11个字符',
            'scene_id.require' => '登录场景不能为空',
    ];

    protected $scene = [
            'user_login' => ['username', 'password', 'captcha'],
            'admin_add'  => ['username'],
            'wx_login'   => ['phone', 'scene_id'],
    ];

    public function __construct()
    {
        parent::__construct();
        // 根据环境变量动态设置验证码规则
        if (isDebug()) {
            unset($this->rule['captcha'], $this->message['captcha.require'], $this->message['captcha.captcha'], $this->scene['user_login'][2]);
        }
    }
}